package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;


    /**
     *  1.直接入库操作  5张表
     *  2.更新操作:  先删除中间表 之后新增中间表
     *  3.检查用户操作是新增还是更新
     *      是更新:  则先删除
     *      最后新增
     */
    @Override
    @Transactional
    public void insertOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {

        //1.判断是否有主键id
        if(apartmentSubmitVo.getId() != null){
            deleteMiddleTable(apartmentSubmitVo.getId());
        }

        //2.公寓新增和更新完成
        //4.根据省市区的id赋值名称
        String pro_name = provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName();
        apartmentSubmitVo.setProvinceName(pro_name);

        String city_name = cityInfoService.getById(apartmentSubmitVo.getCityId()).getName();
        apartmentSubmitVo.setCityName(city_name);

        String district_name = districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName();
        apartmentSubmitVo.setDistrictName(district_name);

        this.saveOrUpdate(apartmentSubmitVo);
        //公寓id
        Long id = apartmentSubmitVo.getId();

        //3.新增中间表数据
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getGraphVoList())){   //记得取 非!!!!
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            List<GraphInfo> graphInfoList = new ArrayList<>();
            //vo 转化为infoList
            graphVoList.forEach(graphVo -> {
                GraphInfo info = new GraphInfo();
                info.setName(graphVo.getName());
                info.setUrl(graphVo.getUrl());
                info.setItemType(ItemType.APARTMENT);
                info.setItemId(id);
                graphInfoList.add(info);
            });
            graphInfoService.saveBatch(graphInfoList);
        }

        //4.新增配套信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getFacilityInfoIds())){
            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            List<ApartmentFacility> facilityList = new ArrayList<>();
            facilityInfoIds.forEach( facilityId -> {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .facilityId(facilityId)
                        .apartmentId(id).build();
                facilityList.add(apartmentFacility);
            });
            facilityService.saveBatch(facilityList);
        }

        //5.新增标签
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getLabelIds())){
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            labelIds.forEach( labelId -> {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(id).build();
                apartmentLabelList.add(apartmentLabel);
            });
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        //6.新增杂费信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getFeeValueIds())){
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            List<ApartmentFeeValue> feeValueList = new ArrayList<>();
            feeValueIds.forEach( feeValueId -> {
                ApartmentFeeValue feeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(id).build();
                feeValueList.add(feeValue);
            });
            feeValueService.saveBatch(feeValueList);
        }
    }

//    删除中间表
    private void deleteMiddleTable(Long id) {
        //1.1先删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoQueryWrapper);

        //1.2 删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelQueryWrapper);

        //1.3 删除公寓杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        feeValueService.remove(apartmentFeeQueryWrapper);

        //1.4 删除配套信息
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        facilityService.remove(facilityLambdaQueryWrapper);
    }

    @Override
    public IPage<ApartmentItemVo> pageInfo(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageInfo(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDataById(Long id) {

        ApartmentDetailVo vo = new ApartmentDetailVo();

//        获取公寓信息
        ApartmentInfo info = this.getById(id);

//        获取配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getDataById(id);

//        获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getDataById(id);

//        获取图片信息
        List<GraphVo> graphVoList = graphInfoMapper.getDataById(ItemType.APARTMENT,id);

//        获取杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.getDataById(id);

        BeanUtils.copyProperties(info,vo);
        vo.setFacilityInfoList(facilityInfoList);
        vo.setLabelInfoList(labelInfoList);
        vo.setGraphVoList(graphVoList);
        vo.setFeeValueVoList(feeValueVoList);

        return vo;
    }

    @Override
    public void deleteInfo(Long id) {
//        删除中间表
        deleteMiddleTable(id);
//        删除主表
        this.removeById(id);
    }
}









